<template>
  <div class="permission" >
    <!--1.搜索框-->
    <el-form ref="queryForm" label-width="86px" label-position="left" :model="queryData" :inline="true" style="white-space: nowrap;">
      <el-row>
        <el-form-item label="字典名称" prop="categoryName">
          <el-input v-model.trim="queryData.categoryName" placeholder="请输入字典项" show-word-limit :style="$queryFormWidth"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="roleList">查询</el-button>
          <el-button @click="resetFields('queryForm')">重置</el-button>
        </el-form-item>
      </el-row>
      <div style="padding: 5px">
        <el-button type="primary" size="mini" @click="toSave('新增')">新增</el-button>
      </div>

    </el-form>
    <!--2.列表-->
    <el-table  v-loading="loading" element-loading-text="数据加载中" :data="tableData" :header-cell-style="{background:'#f0f0f0'}">
      <el-table-column  type="index" label="序号" width="50">
      </el-table-column>
      <el-table-column label="部门名称" width="200">
        <template slot-scope="scope">{{scope.row.departmentName}}</template>
      </el-table-column>
      <el-table-column label="部门类型" width="200">
         <template slot-scope="scope">{{scope.row.departmentType}}</template>
      </el-table-column>
      <el-table-column label="角色名称" width="200">
        <template slot-scope="scope">{{scope.row.roleName}}</template>
      </el-table-column>
      <el-table-column  label="角色类型" width="200">
        <template slot-scope="scope">{{scope.row.roleType}}</template>
      </el-table-column>
      <el-table-column  label="角色状态" width="200">
        <template slot-scope="scope">{{scope.row.enabled==0?"启用":"禁用"}}</template>
      </el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="text" @click.stop="toSave('编辑',scope.row)">编辑</el-button>
          <el-button type="text" @click.stop="toAssign(scope.row)">设置权限</el-button>
          <el-button type="text" @click.stop="toAddUser(scope.row)">分配人员</el-button>
        </template>
       </el-table-column>
    </el-table>
    <!--3.分页-->
    <div style=" background-color: #ffffff;padding: 20px 10px;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pageNumber"
        :page-sizes="[10, 50, 100, 200]"
        :page-size="queryData.pageSize"
        layout=" total,sizes,->,jumper, prev, pager, next"
        :total="totalSize">
      </el-pagination>
    </div>
    <!--4.编辑窗口-->
    <el-dialog :title="title" :visible.sync="saveDialog"  width="30%" append-to-body :close-on-click-modal="false" @close="closeRoleForm">
      <el-form ref="roleForm" label-width="86px" label-position="left" :model="saveData" :rules="saveRules">
        <el-form-item   label="部门" prop="departmentId" >
          <span v-if="saveData.departmentRoleId">{{saveData.departmentName}}</span>
          <el-cascader  v-else :options="treeData" :props="cascadeProps"  :show-all-levels="false"v-model="saveData.departmentId" clearable  ref="cascader"  style="width: 100%"></el-cascader>
        </el-form-item>
        <el-form-item label="角色名称" prop="name">
          <el-input v-model.trim="saveData.roleName" placeholder="请输入部门名称" show-word-limit />
        </el-form-item>
        <el-form-item label="角色类型" prop="state">
          <el-select v-model="saveData.roleType" placeholder="请选择">
            <el-option v-for="item in roleType" :key="item.defId" :label="item.defName" :value="item.defName"/>
          </el-select>
        </el-form-item>
        <el-form-item label="角色状态" prop="enabled">
          <el-select v-model="saveData.enabled" placeholder="请选择">
            <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="saveDialog = false">取 消</el-button>
        <el-button type="primary" @click="editRole">确 定</el-button>
      </span>
    </el-dialog>
    <addUser></addUser>
    <assign ></assign>
  </div>
</template>

<script>
import addUser from './addUser'
import assign from './assign'
export default {
  name: 'permission',
  components: {addUser,assign},
  props: {},
  data() {
    return {
      roleType:[],
      departmentId: '18FBBB22E6304A8BAA07A1BC60FD7358',
      //列表搜索字段
      queryData:{
        departmentId: '18FBBB22E6304A8BAA07A1BC60FD7358',
        categoryName:'',
        //当前页
        pageNumber: 1,
        //每一页条数
        pageSize: 10,
      },
      //列表数据
      tableData: [],
      //是否展示编辑框
      saveDialog: false,
      treeData: [],
      title:'',
      cascadeProps:{
        value: "departmentId",
        label: "name",
        children: "children",
        emitPath:false,
        checkStrictly: true
      },
      // 编辑框数据
      saveData: {
        departmentId: "",
        departmentRoleId: "",
        enabled: "0",
        remark: "",
        roleName: "",
        roleType: "",
        departmentName:''
      },
      saveRules: {
        // categoryName: [
        //   { required: true, message: '请输入字典名称', trigger: 'blur' },
        //   { max: 5, message: '字典名称不能超过10 个字符', trigger: 'blur' }
        // ],
        // state: [
        //   { required: true, message: '请选字典状态', trigger: 'change' }
        // ]
      },
      // 字典状态类型下拉选择
      typeList: [{value: '0', label: '启用'},
                 {value: '1', label: '禁用'}
                ],

      //总条数
      totalSize: 0,

      //是否在加载数据
      loading: false,
      //是否展开右边抽屉
      drawer:false,
      //字典值数据
      def:{},
      // 是否开启调试输出
      debug: true
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() { // 渲染后执行
    // 查询数据
    this.roleList();
    this. departmentTree();
    this.getRoleType();
  },
  methods: {
    //树
    departmentTree(){
      this.$api.department.tree({departmentId:this.departmentId}).then(resp => {
        this.treeData=resp;
      })
    },
    //角色类型
    getRoleType(){
      this.$api.dictionary.getByCategoryCode({categoryCode:'CATEGORY_2'}).then(resp => {
        this.roleType=resp;
      })
    },
    //列表
    roleList(){
      this.loading = true;
      this.$api.permission.list( this.queryData).then(resp => {
        this.tableData = resp.data;
        this.totalSize=  resp.totalSize;
      })
      // 完成赋值后，把加载状态赋值为false
      this.loading = false;
    },
    //弹出编辑窗口
    toSave(type,data){
      this.title=type
      this. saveDialog=true;
      if(data!=undefined){
        this.saveData=JSON.parse(JSON.stringify(data));
      }
    },
    // 点击保存编辑字典项
    editRole() {
      this.$refs.roleForm.validate(async (valid) => {
        if (valid) {
          this.$api.permission.save(this.saveData).then(resp => {
            this.saveDialog = false
            this. roleList()
          })
        } else {
          return false
        }
      })
    },
    //分页
    handleSizeChange(val) {
      this.queryData.pageSize=val;
      this.roleList();
    },
    handleCurrentChange(val) {
      this.queryData.pageNumber=val;
      this.roleList();
    },
    //resetFields
    resetFields(formName){
      this.$refs[formName].resetFields();
    },
    closeRoleForm(){
     this. saveData={
        departmentId: "",
          departmentRoleId: "",
          enabled: "0",
          remark: "",
          roleName: "",
          roleType: "",
          departmentName:''
      }
    },
    toAddUser(data){
      this.$bus.$emit('openAddUser',data)
    },
    toAssign(data){
      this.$bus.$emit('openAssign',data)
    },
  },

}
</script>

<style scoped >
  .permission {
    padding: 20px;
  }
</style>
